//
//  CustomTabBarViewController.m
//  CustomTabBar
//
//  Created by 张琦 on 2016/10/22.
//  Copyright © 2016年 张琦. All rights reserved.
//

#import "CustomTabBarViewController.h"
#import "CustomTabBar.h"
#import "HomeViewController.h"
#import "MeViewController.h"
#import "NotificationViewController.h"
#import "WriteViewController.h"
#import "SubscriptionViewController.h"
#import "WKWebViewController.h"
@interface CustomTabBarViewController ()<CustomTabBarDelegate>
@property (nonatomic, strong) CustomTabBar *customTabBarView;
@property(nonatomic, strong)HomeViewController *homeVc;
@property(nonatomic, strong)SubscriptionViewController *subscriptionVc;
@property(nonatomic, strong)NotificationViewController *notificationVc;
@property(nonatomic, strong)MeViewController *meVc;
@property (nonatomic, strong) UINavigationController *curNavi;
@property (nonatomic, strong) NSMutableArray *arrNavi;
@end

@implementation CustomTabBarViewController

+ (instancetype)shareedCustomTabBarViewController {
    static CustomTabBarViewController *customTabBarVc = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        customTabBarVc= [[CustomTabBarViewController alloc] init];
    });
    return customTabBarVc;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.arrNavi = [NSMutableArray array];

    [self createTabBar];
    [self SetupAllControllers];
}

- (void)createTabBar {
    self.customTabBarView = [[CustomTabBar alloc] initWithFrame:self.tabBar.bounds];
    [self.tabBar addSubview:self.customTabBarView];
    self.customTabBarView.delegate = self;
}

- (void)SetupAllControllers{
    NSArray *titles = @[@"发现", @"关注", @"消息", @"我的"];
    NSArray *images = @[@"icon_tabbar_home~iphone", @"icon_tabbar_subscription~iphone", @"icon_tabbar_notification~iphone", @"icon_tabbar_me~iphone"];
    NSArray *selectedImages = @[@"icon_tabbar_home_active~iphone", @"icon_tabbar_subscription_active~iphone", @"icon_tabbar_notification_active~iphone", @"icon_tabbar_me_active~iphone"];
    
    self.homeVc = [[HomeViewController alloc] init];
    
    self.subscriptionVc = [[SubscriptionViewController alloc] init];
    
    self.notificationVc = [[NotificationViewController alloc] init];
    
    self.meVc = [[MeViewController alloc] init];
    
    NSArray *viewControllers = @[self.homeVc, self.subscriptionVc, self.notificationVc, self.meVc];
    
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *childVc = viewControllers[i];
        [self SetupChildVc:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i]];
    }
}

- (void)SetupChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName{
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:childVc];
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    childVc.title = title;
    [self.customTabBarView addCustomTabBarButtonWithItem:childVc.tabBarItem];
    [self addChildViewController:navi];
    [self.arrNavi addObject:navi];
    if (self.arrNavi.count == 1) {
        self.curNavi = [self.arrNavi firstObject];
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.curNavi.viewControllers.count >= 1) {
        viewController.hidesBottomBarWhenPushed = YES;
        [self.curNavi pushViewController:viewController animated:animated];
    }
}

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *vc = [self.curNavi popViewControllerAnimated:animated];
    return vc;
}
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    return [self.curNavi popToViewController:viewController animated:animated];
}
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    return [self.curNavi popToRootViewControllerAnimated:animated];
}



- (void)tabBar:(CustomTabBar *)tabBar didSelectedButtonFrom:(long)fromBtnTag to:(long)toBtnTag {
    self.selectedIndex = toBtnTag;
    if (self.arrNavi.count) {
        self.curNavi = [self.arrNavi objectAtIndex:toBtnTag];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
